﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace YJKArch.GlobleSetModule
{
	public partial class DlgAssistLevel : System.Windows.Forms.Form
	{
		public DlgAssistLevel(ExternalCommandData cmdData)
		{
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.InitializeComponent();
			this.InitLevelType();
			base.StartPosition = FormStartPosition.Manual;
			if (!this.ReadData())
			{
				base.Location = RevitVersionFuncs.GetRevitTopLeftPoint(cmdData);
			}
		}

		public double Offset()
		{
			return Convert.ToDouble(this.tbOffset.Text);
		}

		public string LevelName()
		{
			return this.tbName.Text;
		}

		public LevelType LevelStyle()
		{
			int selectedIndex = this.cmbType.SelectedIndex;
			return this.m_levelTypes[selectedIndex] as LevelType;
		}

		public void InitLevelType()
		{
			this.m_levelTypes = ElementFiltering.FindSpecialElements(this.m_doc, typeof(LevelType), BuiltInCategory.OST_Levels);
			foreach (Element element in this.m_levelTypes)
			{
				LevelType levelType = element as LevelType;
				this.cmbType.Items.Add(levelType.Name);
			}
			if (this.cmbType.Items.Count > 0)
			{
				this.cmbType.SelectedIndex = 0;
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void KeyEsc()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			DlgAssistLevel.SendMessage(CmdAssistLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			DlgAssistLevel.keybd_event((byte)bVk, 0, dwFlags, 0);
			DlgAssistLevel.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void DlgAssistLevel_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgAssistLevel.SendMessage(CmdAssistLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void DlgAssistLevel_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.KeyEsc();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "AssistLevel.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.cmbType.Text);
			streamWriter.WriteLine(this.tbName.Text);
			streamWriter.WriteLine(this.tbOffset.Text);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private bool ReadData()
		{
			string path = Path.GetTempPath() + "AssistLevel.dat";
			if (!File.Exists(path))
			{
				return false;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			try
			{
				int x = Convert.ToInt32(streamReader.ReadLine());
				int y = Convert.ToInt32(streamReader.ReadLine());
				base.Location = new System.Drawing.Point(x, y);
				string value = streamReader.ReadLine();
				int num = this.cmbType.Items.IndexOf(value);
				if (num > -1)
				{
					this.cmbType.SelectedIndex = num;
				}
				this.m_name = streamReader.ReadLine();
				this.tbName.Text = this.m_name;
				this.m_offset = streamReader.ReadLine();
				this.tbOffset.Text = this.m_offset;
			}
			catch
			{
				return false;
			}
			finally
			{
				streamReader.Close();
				fileStream.Close();
			}
			return true;
		}

		private void DlgAssistLevel_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void tbName_TextChanged(object sender, EventArgs e)
		{
			if (this.tbName.Text.Length == 0)
			{
				TaskDialog.Show("盈建科", "层名不能为空。");
				this.tbName.Text = this.m_name;
				return;
			}
			string text = "`?<>|;:[]{}~\\";
			for (int i = 0; i < this.tbName.Text.Length; i++)
			{
				if (text.IndexOf(this.tbName.Text[i]) > -1)
				{
					TaskDialog.Show("盈建科", "层名中含非法字符。");
					this.tbName.Text = this.m_name;
					return;
				}
			}
			this.m_name = this.tbName.Text;
		}

		private void cmbType_DropDownClosed(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				DlgAssistLevel.SendMessage(CmdAssistLevel._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void DlgAssistLevel_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void tbOffset_Leave(object sender, EventArgs e)
		{
			try
			{
				Convert.ToDouble(this.tbOffset.Text);
			}
			catch (Exception)
			{
				YJKMessageBox.Show("请输入数字");
				this.tbOffset.Text = this.m_offset;
				return;
			}
			this.m_offset = this.tbOffset.Text;
		}

		private void tbOffset_MouseLeave(object sender, EventArgs e)
		{
			try
			{
				Convert.ToDouble(this.tbOffset.Text);
			}
			catch (Exception)
			{
				YJKMessageBox.Show("请输入数字");
				this.tbOffset.Text = this.m_offset;
				return;
			}
			this.m_offset = this.tbOffset.Text;
		}

		private Autodesk.Revit.DB.Document m_doc;

		public string m_name = "夹层";

		public string m_offset = "1000";

		private IList<Element> m_levelTypes;
	}
}
